/******************************************************************************
 * Copyright (c) 2008, Intalio Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Intalio Inc. - initial API and implementation
 *******************************************************************************
 *
 * Date         Author             Changes
 * Apr 25, 2008      Antoine Toulme     Created
 */
package ms.unicorn.ui.borders;

import ms.unicorn.ui.figures.Theme;

import org.eclipse.draw2d.AbstractBorder;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;

/**
 * Scheme border for rounded rectangle shapes.
 *
 *
 * @author <a href="http://www.intalio.com">Intalio Inc.</a>
 * @author <a href="mailto:atoulme@intalio.com">Antoine Toulme</a>
 */
public class RoundedSchemeBorder extends AbstractBorder {
    
    public static final Insets INSETS = new Insets(0, 0, 4, 4);

	protected Theme theme;
        
    public void paint(IFigure fig, Graphics graphics, Insets insets) {
        if (theme.isShadow()) {
	        graphics.setAlpha(theme.getShadowAlpha());
	        graphics.setBackgroundColor(theme.getShadowColor());
	        fillShadow(fig, graphics, insets);
        }
        if (fig.getBackgroundColor().equals(ColorConstants.white)) 
        	graphics.setBackgroundColor(theme.getBkColor());
        else
        	graphics.setBackgroundColor(fig.getBackgroundColor());
       
        if (fig.getForegroundColor().handle == 11579568)
        	graphics.setForegroundColor(theme.getForeColor());
        else
        	graphics.setForegroundColor(fig.getForegroundColor());
        graphics.setAlpha(theme.getBkAlpha());
        
        
        
        Rectangle rt = fig.getBounds().getCopy();
        if (theme.isShadow()) 
        	rt.resize(-4, -4);
        graphics.fillRoundRectangle(rt, theme.getArcWidth(), theme.getArcHeight());
        graphics.drawRoundRectangle(rt, theme.getArcWidth(), theme.getArcHeight());
        
        return;

    }
    
    protected void fillShadow(IFigure fig, Graphics graphics, Insets insets) {
        Rectangle rect = null;
        
        rect = fig.getBounds().getCopy().translate(3, 3)
                .resize(-4, -4);//getPaintRectangle(fig, insets);
        
        graphics.fillRoundRectangle(rect, theme.getArcWidth(), theme.getArcHeight());
    }
    
    public Insets getInsets(IFigure figure) {
        return INSETS;
    }

	public void setTheme(Theme theme) {
		this.theme = theme;
	}
    
}
